package com.beiben.utils.file;

import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;

/**
 * @Description:
 * @Author: Peate
 * @Date: Created in 2020-08-28 9:23
 * @Version 1.0
 * @Modified By:
 */
public class FileTypeUtils {

    public static final String[] DOCUMENT_EXTENSION = {
            "doc", "docx", "txt"
    };
    public static final String[] EXCEL_EXTENSION = {
            "xls", "xlsx"
    };
    public static final String[] POWERPOINT_EXTENSION = {
            "ppt", "pptx"
    };
    public static final String[] RAR_EXTENSION = {
            "zip", "rar"
    };
    public static final String[] CAXA_EXTENSION = {
            "exb","dwg","CATDrawing","STEP"
    };
    public static final String[] PDF_EXTENSION = {
            "pdf"
    };

    private FileTypeUtils() {

    }

    public static int getType(String suffix) {
        System.out.println("文件类型："+suffix);
        if (StringUtils.isBlank(suffix)) {
            throw new BaseException("文件格式无法识别");
        }
        //0:图片
        for (String type : MimeTypeUtils.IMAGE_EXTENSION) {
            if (type.equals(suffix)) {
                return 0;
            }
        }

        //1：视频
        for (String type : MimeTypeUtils.MEDIA_EXTENSION) {
            if (type.equals(suffix)) {
                return 1;
            }
        }
        //2：文档
        for (String type : FileTypeUtils.DOCUMENT_EXTENSION) {
            if (type.equals(suffix)) {
                return 2;
            }
        }
        //3：表格
        for (String type : FileTypeUtils.EXCEL_EXTENSION) {
            if (type.equals(suffix)) {
                return 3;
            }
        }
        //4：演示文本
        for (String type : FileTypeUtils.POWERPOINT_EXTENSION) {
            if (type.equals(suffix)) {
                return 4;
            }
        }
        //压缩文件
        for (String type : FileTypeUtils.RAR_EXTENSION) {
            if (type.equals(suffix)) {
                return 5;
            }
        }
        //CAXA
        for (String type : FileTypeUtils.CAXA_EXTENSION) {
            if (type.equals(suffix)) {
                return 6;
            }
        }
        //PDF
        for (String type : FileTypeUtils.PDF_EXTENSION) {
            if (type.equals(suffix)) {
                return 7;
            }
        }
        throw new BaseException("文件格式错误");
    }
}
